var orderVm;
var pageIndex=0;
var pageSize=50;
var getDataListHttp;
var initShopStock;
$(function(){

    getDataListHttp=function(isRefresh){
    	if(!orderVm.enterName){
    		orderVm.enterList=[];
    		return;
    	}
        var param="&name="+orderVm.enterName;
        _getHttpsData2('/enterprise/listByIsWaterPoint?isWaterPoint=1'+param,'',
           function(res){
               console.log(param+JSON.stringify(res))
               orderVm.enterList=res
			   
           },
           function(err){         	
                vant.Toast.fail("网络异常"+err.readyState+":"+err.statusText+"("+err.status+")");
           }
       );
    }
	
	initShopStock=function(){
		orderVm.orderList=[];
	    var param="?enterpriseId="+orderVm.pickedEnter.enterpriseId;
	    _getHttpsData2('/enterprise/getNum'+param,'',
	       function(res){
	            console.log(param+JSON.stringify(res));
	            orderVm.stockNum=res;
	       },
	       function(err){           	
	       }
	   );
	}
    
    initOrderList=function(){
    	orderVm.orderList=[];
		orderVm.loading = true;
		orderVm.finished = false;
        var param="?mobile="+orderVm.phoneNum+"&enterpriseId="+orderVm.pickedEnter.enterpriseId;
        _getHttpsData2('/order/listByMobile'+param,'',
           function(res){
                console.log(param+JSON.stringify(res));
                orderVm.orderList=res;
				orderVm.loading = false;
                orderVm.finished = true;
           },
           function(err){           	
                vant.Toast.fail("网络异常"+err.readyState+":"+err.statusText+"("+err.status+")");
                orderVm.loading = false;
                orderVm.finished = true;
           }
       );
    }

	pickEnter=function(i){
		var enterInfo=orderVm.enterList[i];
		orderVm.pickedEnter=enterInfo;
		localStorage.setItem("pickedEnter", JSON.stringify(enterInfo));
		orderVm.enterName=enterInfo.name;
		orderVm.enterList=[];
		initShopStock();
	}
	
	checkOrder=function(i){
		if(!orderVm.pickedEnter){
			vant.Toast.fail("请先选择企业");
			return;
		}
		var checkConfirm=confirm("确认");
	    if(checkConfirm){
			var orderInfo=orderVm.orderList[i];
			var param="?enterpriseId="+orderVm.pickedEnter.enterpriseId+'&mOrderId='+orderInfo.mOrderId+'&mobile='+orderInfo.mobile;
	        _getHttpsData2('/cancel/confirm'+param,'',
	           function(res){
	                console.log(param+JSON.stringify(res));
	                vant.Toast.fail(res.msg);
	                initOrderList();
	           },
	           function(err){           	
	                vant.Toast.fail("网络异常"+err.readyState+":"+err.statusText+"("+err.status+")");
	                
	           }
	       );
	    }
	}
	

    orderVm = new Vue({
        el:'#order_id',
        data:{
            activeTop:0,
            loading: true,
            finished: true,
            refreshing: false,
            dataIsNull:false,
            orderList:[],
            enterList:[],
            pickedEnter:'',
            enterName:'',
            phoneNum:'',
			stockNum:0
        },
        methods:{
            tabBeforeChange:function(position){
                this.onRefresh();
                return true;
            },
            showStatusName:function(status){
                var obj={
                    "0":"待付款",
                    "1":"待提货"
                }
                return obj[status]==undefined ? '已完成':obj[status];
            },
            onLoad:function(){
                var pickedEnter=localStorage.getItem("pickedEnter");
                if(pickedEnter){
                	orderVm.pickedEnter=JSON.parse(pickedEnter);
                	orderVm.enterName=orderVm.pickedEnter.name;
            	}
                console.log('pickedEnter:'+pickedEnter)
            },
            onRefresh:function(){
                this.refreshing=true;
                // 清空列表数据
                this.finished = false;
                // 重新加载数据
                // 将 loading 设置为 true，表示处于加载状态
                this.loading = true;
                this.onLoad();
            },
            refreshData:function(data){
                this.dataList=data;
                this.nextTick(data);
            },
            appendData:function(data){
                this.dataList=this.dataList.concat(data);
                this.nextTick(data);
            },
            nextTick:function(data){
                this.loading=false;
                this.finished=data!=undefined && data.length>=pageNum?false:true;
                this.dataIsNull=this.dataList.length==0 ? true:false;
                this.$nextTick(function(){
                    api.parseTapmode();
                    api.hideProgress();
                })
            },
        }
    });
});